package resp

import (
	"github.com/gin-gonic/gin"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
	"HeadLineNews/proto_models/common"
	"net/http"
)

// InvalidToken token 为空或者异常
func InvalidToken(c *gin.Context) {
	c.ProtoBuf(http.StatusOK, &common.Response{
		Code:    codeInvalidToken,
		Message: respMessageMap[codeInvalidToken],
		Data:    nil,
	})
}

// PermissionDenied 权限不足
func PermissionDenied(c *gin.Context) {
	c.ProtoBuf(http.StatusOK, &common.Response{
		Code:    codePermissionDenied,
		Message: respMessageMap[codePermissionDenied],
		Data:    nil,
	})
}

// InvalidParam 无效的参数
func InvalidParam(c *gin.Context, errsMap map[string]string) {
	data, _ := anypb.New(&common.ErrorData{ErrorData: errsMap}) // 打包错误信息
	c.ProtoBuf(http.StatusOK, &common.Response{
		Code:    codeInvalidParam,
		Message: respMessageMap[codeInvalidParam],
		Data:    data,
	})
}

// UnprovedParam 不符合业务逻辑的参数
func UnprovedParam(c *gin.Context, message string) {
	c.ProtoBuf(http.StatusOK, &common.Response{
		Code:    codeUnprovedParam,
		Message: message,
		Data:    nil,
	})
}

// ServerBusy API 抛出的不为 nil 的 error
func ServerBusy(c *gin.Context) {
	c.ProtoBuf(http.StatusOK, &common.Response{
		Code:    codeServerBusy,
		Message: respMessageMap[codeServerBusy],
		Data:    nil,
	})
}

// ServerPanic API 内部抛出 panic
func ServerPanic(c *gin.Context) {
	c.ProtoBuf(http.StatusOK, &common.Response{
		Code:    codeServerPanic,
		Message: respMessageMap[codeServerPanic],
		Data:    nil,
	})
}

// Success 成功响应
func Success(c *gin.Context, protoMsg proto.Message) {
	data, _ := anypb.New(protoMsg)
	c.ProtoBuf(http.StatusOK, &common.Response{
		Code:    codeSuccess,
		Message: respMessageMap[codeSuccess],
		Data:    data,
	})
}
